package com.efast.cafe.portal.service.doc.impl;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.doc.DocGroupQueryBean;
import com.efast.cafe.portal.dao.doc.DocGroupDao;
import com.efast.cafe.portal.entity.doc.DocGroup;
import com.efast.cafe.portal.service.doc.IDocGroupService;
import com.github.pagehelper.PageHelper;
@Service
@Transactional
public class DocGroupServiceImpl implements IDocGroupService {

	private DocGroupDao docGroupDao;
	
	@Autowired
	public void setDocGroupDao(DocGroupDao docGroupDao) {
		this.docGroupDao = docGroupDao;
	}

	@Override
	public DocGroupQueryBean queryGroupList(DocGroupQueryBean queryBean) {
		PageHelper.startPage(queryBean.getStart(),queryBean.getLength());
		queryBean.setResultList(docGroupDao.queryGroupList(queryBean));
		queryBean.setTotalRows(docGroupDao.queryGroupCount(queryBean));
		return queryBean;
	}

	@Override
	public void addGroup(DocGroup group) {
		group.setCreateUser(SpringWebUtils.getRequestUser());
		group.setCompanyCode(SpringWebUtils.getRequestCompany());
		//验证名称是否重复
		if(docGroupDao.queryNameRepeat(group.getId(),group.getGroupName(),group.getCompanyCode())>0){
			throw new ServiceException("DATA_REPEAT");
		}
		docGroupDao.addGroup(group);
	}

	@Override
	public void updateGroup(DocGroup group) {
		group.setCompanyCode(SpringWebUtils.getRequestCompany());
		//验证名称是否重复
		if(docGroupDao.queryNameRepeat(group.getId(),group.getGroupName(),group.getCompanyCode())>0){
			throw new ServiceException("DATA_REPEAT");
		}
		docGroupDao.updateGroup(group);
	}

	@Override
	public void deleteGroup(String pid) {
		//验证有没有下级
		if(pid.startsWith("group")){
			Integer groupId=Integer.parseInt(pid.replaceFirst("group_", ""));
			
			if(docGroupDao.queryFilepCountByGroup(groupId)>0){
				throw new ServiceException("包含文件夹不能直接删除");
			}
			docGroupDao.deleteGroup(groupId);
			
		}else if(pid.startsWith("folder")){
			Integer fileId=Integer.parseInt(pid.replaceFirst("folder_", ""));
			if(docGroupDao.queryFilepCountByFile(fileId)>0){
				throw new ServiceException("包含文件夹不能直接删除");
			}
			docGroupDao.deleteFold(fileId);
		}
		
		
	}

}
